//
//  TodayViewController.m
//  MemoryTasks
//
//  Created by Philippe Bouamriou on 11/05/10.
//  Copyright 2010 Philippe Bouamriou. All rights reserved.
//

#import "TodayViewController.h"
#import "GCCalendarEvent.h"
#import "GCCalendar.h"
#import "TaskEditionViewController.h"


@implementation TodayViewController

@synthesize navController;


 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
   UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                                                  target:self 
                                                                                  action:@selector(addButtonPressed)];
   
   self.navigationItem.rightBarButtonItem=barButtonItem;
   
	dataSource = self;
	delegate = self;
   [super viewDidLoad];
}

- (void)loadView {
   dataSource = self;
	delegate = self;
   [super loadView];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
   [super viewDidUnload];
   // Release any retained subviews of the main view.
   // e.g. self.myOutlet = nil;
   self.navController = nil;
}



#pragma mark -
#pragma mark Actions

- (void)addButtonPressed {
   TaskEditionViewController *taskEditionViewCtrl = [[TaskEditionViewController alloc] 
                                                             initWithNibName:@"TaskEditionViewController" 
                                                             bundle:nil];
   taskEditionViewCtrl.navController = navController;
   [navController pushViewController:taskEditionViewCtrl animated:YES];
   
}

#pragma mark -
#pragma mark Implementation of protocol GCCalendarDataSource

- (NSArray *)calendarEventsForDate:(NSDate *)theDate {
	NSMutableArray *events = [NSMutableArray array];
	
	NSDateComponents *components = [[NSCalendar currentCalendar] components:
                                   (NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit)
                                                                  fromDate:theDate];
	[components setSecond:0];
	
	// create 5 calendar events that aren't all day events
	for (NSInteger i = 0; i < 5; i++) {
		GCCalendarEvent *event = [[GCCalendarEvent alloc] init];
		event.color = [[GCCalendar colors] objectAtIndex:i];
		event.allDayEvent = NO;
		event.eventName = [event.color capitalizedString];
		event.eventDescription = event.eventName;
		
		[components setHour:12 + i];
		[components setMinute:0];
		
		event.startDate = [[NSCalendar currentCalendar] dateFromComponents:components];
		
		[components setMinute:50];
		
		event.endDate = [[NSCalendar currentCalendar] dateFromComponents:components];
		
		[events addObject:event];
	}
	
   /*
    // create an all day event
    GCCalendarEvent *event = [[GCCalendarEvent alloc] init];
    event.allDayEvent = YES;
    event.eventName = @"All Day Event";
    [events addObject:event];
    [event release];
    
    
    // create an all day event
    event = [[GCCalendarEvent alloc] init];
    event.allDayEvent = YES;
    event.eventName = @"All Day Event (2)";
    [events addObject:event];
    [event release];
    */
	
	return events;
}

#pragma mark -
#pragma mark Implementation of protocol GCCalendarDelegate

- (void)calendarTileTouchedInView:(GCCalendarView *)view withEvent:(GCCalendarEvent *)event {
	NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)calendarViewAddButtonPressed:(GCCalendarView *)view {
	NSLog(@"%s", __PRETTY_FUNCTION__);
}


@end
